//
//  UserCenterController.m
//  DriverBus
//
//  Created by MacMini20170218 on 2017/3/28.
//  Copyright © 2017年 JiYang. All rights reserved.
//

#import "UserCenterController.h"
#import "AppDelegate.h"

@interface UserCenterController ()<UIAlertViewDelegate>

@end

@implementation UserCenterController{
    AppDelegate *appDelegate;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置返回导航颜色
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
    self.name.text = appDelegate.driver.name;
    self.account.text = appDelegate.driver.account;
    self.sex.text = appDelegate.driver.sex;
    self.number.text= appDelegate.driver.number;
    [self loadDriverImage];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //消除cell选择痕迹
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section==1){
        switch (indexPath.row) {
            case 0:{
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"退出登录" message:@"确定退出登录吗?" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
                [alert show];}
                break;
            default:
                break;
        }
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            //确定退出
            [appDelegate.driver saveToData:NO];
            [self.delegate exitLog];
            [self.navigationController popViewControllerAnimated:YES];
            break;
        default:
            break;
    }
}

#pragma mark 加载图片
- (void)loadDriverImage{
    [self.imageLoading startAnimating];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:appDelegate.driver.imageUrl];
    NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        [self.imageLoading stopAnimating];
        if(error==nil){
            UIImage *image = [UIImage imageWithData:data];
            if(image){
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.userLog.image = image;
                    //以下两行设置圆角
                    self.userLog.layer.cornerRadius = 10;
                    //将多余的部分切掉
                    self.userLog.layer.masksToBounds = YES;
                });
            }
        }else{
            NSLog(@"下载图片错误:%@",error);
        }
    }];
    [task resume];
}

@end
